package com.softeem.service.Impl;

import com.softeem.entity.*;
import com.softeem.mapper.StaffMapper;
import com.softeem.mapper.UserMapper;
import com.softeem.mapper.WorkOrderMapper;
import com.softeem.model.LayuiPagingResult;
import com.softeem.model.ParamModel;
import com.softeem.model.WorkOrderModel;
import com.softeem.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Autowired
    private WorkOrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StaffMapper staffMapper;

    @Override
    public boolean addWorkOrder(WorkOrder workOrder) {
        try {
            return orderMapper.insertSelective(workOrder) > 0;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public WorkOrderModel getWorkOrderById(Integer id) {
        return orderMapper.selectModelById(id);
    }

    @Override
    public LayuiPagingResult<WorkOrderModel> getOrdersDynamical(ParamModel params) {
        LayuiPagingResult<WorkOrderModel> result = new LayuiPagingResult<>();
        WorkOrderExample example = new WorkOrderExample();
        WorkOrderExample.Criteria criteria = example.createCriteria();

        //封装example的where条件
        if(params.getId() != null){
            criteria.andUserIdEqualTo(params.getId());
        }
        if(params.getUserName() != null && ! params.getUserName().equals("")){
            String userName = params.getUserName();
            UserExample userExample = new UserExample();
            userExample.createCriteria().andNickNameLike("%"+userName+"%");
            List<User> users = userMapper.selectByExample(userExample);
            if(users.size() > 0){
                List<Integer> userIds = new ArrayList<>();
                for (User user : users) {
                    userIds.add(user.getId());
                }
                criteria.andUserIdIn(userIds);
            }else {
                criteria.andUserIdIn(Arrays.asList(-1));
            }
        }
        if(params.getTitle() != null){
            criteria.andTitleLike("%"+params.getTitle()+"%");
        }
        if(params.getContent() != null){
            criteria.andContentLike("%"+params.getContent()+"%");
        }
        if(params.getPath() != null){
            criteria.andPathLike(params.getPath());
        }
        if(params.getCreateTime() != null){
            criteria.andCreateTimeEqualTo(params.getCreateTime());
        }
        if(params.getStaffName() != null && ! params.getStaffName().equals("")){
            String staffName = params.getStaffName();
            StaffExample staffExample = new StaffExample();
            staffExample.createCriteria().andNickNameLike("%"+staffName + "%");
            List<Staff> staffs= staffMapper.selectByExample(staffExample);
            if(staffs.size() > 0){
                List<Integer> staffIds = new ArrayList<>();
                for (Staff staff : staffs) {
                    staffIds.add(staff.getId());
                }
                criteria.andStaffIdIn(staffIds);
            }else {
                criteria.andStaffIdIn(Arrays.asList(-1));
            }

        }
        if(params.getNowTime() != null){
            criteria.andNowTimeEqualTo(params.getNowTime());
        }
        if(params.getIsAllot() != null){
            criteria.andIsAllotEqualTo(params.getIsAllot());
        }
        if(params.getIsUreplay() != null){
            criteria.andIsUreplayEqualTo(params.getIsUreplay());
        }
        if(params.getIsSreplay() != null){
            criteria.andIsSreplayEqualTo(params.getIsSreplay());
        }
        if(params.getIsFinish() != null){
            criteria.andIsFinishEqualTo(params.getIsFinish());
        }

        //封装排序条件
        if(params.getOrder() != null){
            example.setOrderByClause(params.getOrder());
        }

        //封装分页参数
        Integer index=0,len=6;
        if(params.getPage() !=null && params.getLimit() != null){
            len = params.getLimit();
            index = (params.getPage()-1)*len;
        }

        //封装结果result
        result.setCode(0);
        result.setMsg("");
        result.setCount((long) orderMapper.selectModelsByExample(example).size());
        result.setData(orderMapper.selectModelsByExampleLimit(example,index,len));

        return result;
    }
}
